/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#ifndef __VISUAL_KERNEL_H
#define __VISUAL_KERNEL_H

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>

#include <directfb.h>

#define DFB_CHECK(x...) {DFBResult e = x; if (e != DFB_OK) {cout << "- directfb error\n\t"; DirectFBErrorFatal(#x, e);}}

using namespace std;

class Event
{
private:
	int devid;
	string devname;
	int type;
	int key;
	int minx, maxx, x;
	int miny, maxy, y;
	int button;

public:
	const static int keypress = 0;
	const static int keyrelease = 1;
	const static int axismotion = 2;
	const static int buttonpress = 3;
	const static int buttonrelease = 4;

	Event();
	Event setKeyPress(int k);
	Event setKeyRelease(int k);
	Event setAxisMotion(int _minx, int _x, int _maxx, int _miny, int _y, int _maxy);
	Event setButtonPress(int b);
	Event setButtonRelease(int b);
	int setDevice(int id, string name);
	int getType();
	int getKey();
	int getMinX();
	int getX();
	int getMaxX();
	int getMinY();
	int getY();
	int getMaxY();
	int getButton();
	string display();
};

DFBEnumerationResult enumerateInputDevice(unsigned int id, DFBInputDeviceDescription dsc, void *data);

class Kernel
{
private:
	IDirectFB *dfb;
	IDirectFBSurface *pri;
	IDirectFBEventBuffer *ebuf;

	int width;
	int height;

	map<string, IDirectFBFont *> fonts;

	map<unsigned int, DFBInputDeviceDescription> devs;

public:
	Kernel();
	int init(int *argc, char ***argv);
	int halt();
	int getWidth();
	int getHeight();
	int rect(int x0, int y0, int x1, int y1, int r, int g, int b, int a);
	int line(int x0, int y0, int x1, int y1, int r, int g, int b, int a);
	int imageGetSize(string path, int &w, int &h);
	int image(int x, int y, string path);
	int loadFont(string name, string path, int size);
	int unloadFont(string name);
	int getTextWidth(string font, string text);
	int text(int x0, int y0, int r, int g, int b, int a, string font, string s);
	int update();
	int update(int x0, int y0, int x1, int y1);
	vector<Event> waitForEvents();
	IDirectFB *getDFB();
};

#endif
